﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.Report
{
    public partial class ContractTrack : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ContractBll bll = new ContractBll();
                lstbxCustomer.DataSource = bll.GetCustomerTableByUserID(CurrentUserID);
                lstbxCustomer.DataTextField = "CustomerName";
                lstbxCustomer.DataValueField = "CustomerID";
                lstbxCustomer.DataBind();

                for (int i = 2000; i < 2020; i++)
                {
                    lstbxYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

            }
        }

        private void LoadContractTrack(List<Guid> customers,List<string> Years)
        {
            //set reference
            ReportViewer ReportViewer = ReportViewer1;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath = strRootPath + "Report\\rdlc\\ReportContractTrack.rdl";
            strFilePath = "report/rdlc/Report1.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.FullPage;
            ReportViewer.AsyncRendering = false;

            //set report viewer work mode
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;

            //get dataset  
            ContractBll bll = new ContractBll();
            DataSet ds = bll.GetContractTrack(customers, Years);
            ds.DataSetName = "ErpSystemDataSet_VW_Report_ContractTrack";
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "ErpSystemDataSet_VW_Report_ContractTrack";
            rds.Value = ds.Tables[0];
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportViewer.LocalReport.Refresh();

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            List<Guid> Customers = new List<Guid>();
            List<string> Years = new List<string>();

            foreach (ListItem item in lstbxCustomer.Items)
            {
                if (item.Selected)
                {
                    Customers.Add(new Guid(item.Value));
                }
            }

            foreach (ListItem item in lstbxYear.Items)
            {
                if (item.Selected)
                {
                    Years.Add(item.Value);
                }
            }

            if (Customers.Count == 0 || Years.Count == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('请选择查询条件');", true);
                return;
            }
            LoadContractTrack(Customers,Years);
        }

    }
}
